package com.example.rydwlib.fragment


import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.baselib.activity.ChooseNickActivity
import com.example.baselib.constants.MyConstants
import com.example.baselib.databean.Unitinfo
import com.example.baselib.fragment.BaseFragment

import com.example.rydwlib.R
import com.example.rydwlib.RydwViewModel
import com.example.rydwlib.adapter.RyMainAdapter
import com.example.rydwlib.bean.RyMainData
import kotlinx.android.synthetic.main.fragment_rydw_main.*
import kotlinx.android.synthetic.main.ry_main_item.*
import java.lang.Exception
import java.util.ArrayList

/**
 * A simple [Fragment] subclass.
 */
class RydwMainFragment : BaseFragment<RydwViewModel>() {
    private val mainAdapter:RyMainAdapter by lazy { RyMainAdapter(R.layout.ry_main_item) }
    private lateinit var empty:View
    override fun providerVMClass(): Class<RydwViewModel>? = RydwViewModel::class.java

    override fun contentLayout(): Int =R.layout.fragment_rydw_main

    override fun initView() {
        empty = LayoutInflater.from(context).inflate(R.layout.empty_item, null, false)
        mainAdapter.emptyView=empty
        ry_main_srl.setColorSchemeColors(resources.getColor(R.color.colorDBlue))
        ry_main_srl.setOnRefreshListener {
            viewModel.isLoad=false
            viewModel.loadRyMainData() }
        ry_main_rcy.adapter=mainAdapter
        ry_main_rcy.layoutManager =  LinearLayoutManager(activity, LinearLayoutManager.VERTICAL,false)
        mainAdapter.setOnItemChildClickListener { adapter, view, position ->
            val bundle = Bundle()
            when(view.id){
                R.id.ry_item_ime_ly -> {
                    if (ry_item_ime.text.toString()=="0") return@setOnItemChildClickListener
                    bundle.putInt("type", 1)
                    Navigation.findNavController(view)
                        .navigate(R.id.action_ry_navigation_main_to_ryMainPersonFragment, bundle)
                }
                R.id.ry_item_special_ly -> {
                    if (ry_item_special.text.toString()=="0") return@setOnItemChildClickListener
                    bundle.putInt("type", 2)
                    Navigation.findNavController(view)
                        .navigate(R.id.action_ry_navigation_main_to_ryMainPersonFragment, bundle)
                }
                R.id.ry_item_lead_ly -> {
                    if (ry_item_lead.text.toString()=="0") return@setOnItemChildClickListener
                    bundle.putInt("type", 3)
                    Navigation.findNavController(view)
                        .navigate(R.id.action_ry_navigation_main_to_ryMainPersonFragment, bundle)
                }
                R.id.ry_item_over_time_ly -> {
                    if (ry_item_over_time.text.toString()=="0") return@setOnItemChildClickListener
                    bundle.putInt("type", 1)
                    Navigation.findNavController(view)
                        .navigate(R.id.action_ry_navigation_main_to_ryMainAlarmFragment, bundle)
                }
                R.id.ry_item_over_person_ly -> {
                    if (ry_item_over_person.text.toString()=="0") return@setOnItemChildClickListener
                    bundle.putInt("type", 2)
                    Navigation.findNavController(view)
                        .navigate(R.id.action_ry_navigation_main_to_ryMainAlarmFragment, bundle)
                }
                R.id.ry_item_call_ly -> {
                    if (ry_item_call.text.toString()=="0") return@setOnItemChildClickListener
                    bundle.putInt("type", 3)
                    Navigation.findNavController(view)
                        .navigate(R.id.action_ry_navigation_main_to_ryMainAlarmFragment, bundle)
                }
                R.id.ry_item_special_err_ly -> {
                    if (ry_item_special_err.text.toString()=="0") return@setOnItemChildClickListener
                    bundle.putInt("type", 4)
                    Navigation.findNavController(view)
                        .navigate(R.id.action_ry_navigation_main_to_ryMainAlarmFragment, bundle)
                }
                R.id.ry_item_sys_ly -> {
                    if (ry_item_sys.text.toString()=="0") return@setOnItemChildClickListener
                    bundle.putInt("type", 5)
                    Navigation.findNavController(view)
                        .navigate(R.id.action_ry_navigation_main_to_ryMainAlarmFragment, bundle)
                }
            }
        }
        ry_main_nickname.setOnClickListener {
            if (MyConstants.userData.usertype>1){
                startActivityForResult(Intent(activity, ChooseNickActivity::class.java),777)
            }
        }
    }

    override fun initData() {
        if (!viewModel.getUnitInfo().isNullOrEmpty()){
            Log.i("+++++++++unitcode",viewModel.getUnitInfo()["unitcode"])
            ry_main_nickname.text=viewModel.getUnitInfo()["unitname"]
        }else if (!viewModel.getUserData().unitinfo.isNullOrEmpty()&&viewModel.getUserData().usertype==1){
            val map:HashMap<String,String> =HashMap()
            map["unitname"]=viewModel.getUserData().unitinfo[0].nickname
            map["unitcode"]=viewModel.getUserData().unitinfo[0].unitcode
            viewModel.setUnitInfo(map)
            ry_main_nickname.text=viewModel.getUnitInfo()["unitname"]
        } else{
            empty.findViewById<TextView>(R.id.empty_msg).text="无煤矿信息"
            return
        }

        viewModel.getMainData().observe(this, Observer {
            val list = ArrayList<RyMainData>()
            list.add(it)
            mainAdapter.setNewData(list)
            ry_main_empty.visibility=View.INVISIBLE
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
        super.onActivityResult(requestCode, resultCode, intent)
        if (resultCode==0&&requestCode==777){
            val map:HashMap<String,String> =HashMap()
            if (intent != null) {
                map["unitname"]= intent.getStringExtra("unitname")
                map["unitcode"]=intent.getStringExtra("unitcode")
                viewModel.setUnitInfo(map)
                ry_main_nickname.text=map["unitname"]
                viewModel.loadRyMainData()
            }
        }
    }

    override fun requestFinally(it: Int?) {
        super.requestFinally(it)
        ry_main_srl.isRefreshing=false
    }

    override fun requestError(it: Exception?) {
        ry_main_empty.visibility=View.VISIBLE
        if (it?.message=="暂无数据"){
            ry_main_empty.findViewById<TextView>(R.id.err_txt).text=it?.message
            ry_main_empty.findViewById<TextView>(R.id.err_txt).textSize=18f
            ry_main_empty.findViewById<TextView>(R.id.err_msg).visibility=View.INVISIBLE
//            ry_main_empty.findViewById<ImageView>(R.id.err_img).visibility=View.INVISIBLE
        }else{
            super.requestError(it)
            ry_main_empty.findViewById<TextView>(R.id.err_msg).text=it?.message
        }
        ry_main_empty.findViewById<Button>(R.id.err_rest_btn).setOnClickListener {  viewModel.loadRyMainData() }
    }

}
